package pers.ree.demo.concurrent;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class SynchronousQueueDemo {

    public static void main(String[] args) {
        SynchronousQueue<Integer> queue = new SynchronousQueue();
        Thread producer = new Thread(()->{
//            try {
//                queue.put(1);
//                System.out.println("produced: 1");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            boolean result = queue.offer(1);
            System.out.println("produced: 1, result: "+result);
        });

        producer.start();

        try {
            TimeUnit.MILLISECONDS.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread consumer = new Thread(()->{
//            try {
//                int element = queue.take();
//                System.out.println("consumed: " + element);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            Integer element = queue.poll();
            System.out.println("consumed: " + element);
        });
        consumer.start();
    }
}
